using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XFABManager;

namespace XFGameFramework
{

    public class LoadPanelRequest<T> : CustomAsyncOperation<LoadPanelRequest<T>> where T : Panel
    {

        public T panel;

        internal IEnumerator LoadPanel(Module module,  string asset_name, UIType type = UIType.UI, Transform parent = null, params object[] param)
        {
            parent = PanelManager.GetParent(module, parent, type);

            if (parent == null)
            {
                Completed("parent is null!");
                yield break;
            }

            LoadViewRequest<T> request = ViewManager.LoadViewAsync<T>(module, asset_name, parent, param);
            yield return request;
            if (!string.IsNullOrEmpty(request.error))
                yield break;

            panel = request.view;

            if (panel == null)
            {
                Completed(string.Format("加载panel失败!:{0}",request.error));
                yield break;
            }
            panel.transform.localPosition = Vector3.zero;
            // 设置层级

            if (panel.IsMyselfAddCanvas)
            {
                panel.SortingOrder = PanelManager.GetUIOrder(type);
                panel.UIType = type;
                PanelManager.AddOrder(type, panel, panel.SortingOrder);
            }

            PanelManager.AddPanelQueue(panel);

            Completed();

        }

    }
}

